﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.Tool
{
    /// <summary>
    /// 功能：随机数算法
    /// </summary>
    public class RamdomTool
    {
        #region 取得随机数
        /// <summary>
        /// 得到不重复的随机数
        /// </summary>
        /// <param name="sum">随机数的数量</param>
        /// <param name="min">随机数的最小值</param>
        /// <param name="max">随机数的最大值</param>
        /// <returns></returns>
        public static int[] GetRandoms(int sum, int min, int max)
        {
            if (min == max)
            {
                return new[] {min};
            }
            if (sum > max+1-min)
            {
                Debug.Log("输入的参数有误"+"sum:"+sum+"max:"+max);
                return null;
            }
            int[] arr = new int[sum];
            int j = 0;
            //表示键和值对的集合。
            Hashtable hashtable = new Hashtable();
            //Random rm0 = new Random();
            //Random rm1 = new Random();
            while (hashtable.Count < sum)
            {
                //返回一个min到max之间的随机数
                switch (Random.Range(0,155))
                {
                    case 1:
                        break;
                }
                int nValue = Random.Range(min, max + 1);
                // 是否包含特定值
                if (!hashtable.ContainsValue(nValue))
                {
                    //把键和值添加到hashtable
                    hashtable.Add(nValue, nValue);
                    arr[j] = nValue;
                    j++;
                }
            }
            return arr;
        }


        /// <summary>
        /// 洗牌算法
        /// 参考 http://blog.csdn.net/robinjwong/article/details/18261393
        /// </summary>
        /// <param name="array"></param>
        /// <param name="length"></param>
        public static void Shuffle<T>(List<T> array)
        {
            // Debug.Log("----随机了题目的排列顺序----");
            for (int i = 0; i < array.Count; i++)
            {
                int j = UnityEngine.Random.Range(i, array.Count);
                T temp = array[j];
                array[j] = array[i];
                array[i] = temp;
            }
        }
        #endregion 
    }
}